package com.example.demo.controller;

import com.example.demo.entity.Course;
import com.example.demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; // 导入 HttpStatus
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程信息 RESTful API 接口
 */
@RestController
@RequestMapping("/api/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 获取所有课程列表
     */
    @GetMapping
    public ResponseEntity<List<Course>> getAllCourses() {
        List<Course> courses = courseService.getAllCourses();
        return ResponseEntity.ok(courses); // HTTP 200 OK
    }

    /**
     * 根据 ID 获取单个课程
     */
    @GetMapping("/{id}")
    public ResponseEntity<Course> getCourseById(@PathVariable("id") String courseId) {
        Course course = courseService.getCourseById(courseId);
        if (course != null) {
            return ResponseEntity.ok(course); // HTTP 200 OK
        } else {
            // 使用 ResponseEntity.status(HttpStatus).build() 或 .body(null)
            // 如果返回类型是 Course，需要返回 null 或使用泛型匹配
            // 这里使用 noContent() 或 notFound() 更简洁
            return ResponseEntity.notFound().build(); // HTTP 404 Not Found
        }
    }
    // GET /api/courses?semesterId={id} - 按学期筛选课程
    @GetMapping(params = "semesterId")
    public List<Course> getCoursesBySemester(@RequestParam("semesterId") String semesterId) {
        return courseService.findCoursesBySemesterId(semesterId);
    }

    /**
     * 新增课程
     */
    @PostMapping
    public ResponseEntity<String> addCourse(@RequestBody Course course) {
        int result = courseService.addCourse(course);
        if (result > 0) {
            // 修正: 使用 status().body()，它返回一个完整的 ResponseEntity<String>
            return ResponseEntity.status(HttpStatus.CREATED).body("Course added successfully. ID: " + course.getCourseId()); // HTTP 201 Created
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Failed to add course due to internal error."); // HTTP 400 Bad Request
        }
    }

    /**
     * 更新课程信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<String> updateCourse(@PathVariable("id") String courseId, @RequestBody Course course) {
        course.setCourseId(courseId); // 确保 ID 正确
        int result = courseService.updateCourseInfo(course);
        if (result > 0) {
            return ResponseEntity.ok("Course updated successfully."); // HTTP 200 OK
        } else if (result == 0) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Course not found with ID: " + courseId); // HTTP 404 Not Found
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to update course."); // HTTP 500
        }
    }

    /**
     * 删除课程
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteCourse(@PathVariable("id") String courseId) {
        int result = courseService.deleteCourse(courseId);
        if (result > 0) {
            return ResponseEntity.ok("Course deleted successfully."); // HTTP 200 OK
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Course not found with ID: " + courseId); // HTTP 404 Not Found
        }
    }

    @GetMapping("/teacher/{teacherId}")
    public ResponseEntity<List<Course>> getCoursesByTeacherId(@PathVariable("teacherId") Integer teacherId) {
        List<Course> courses = courseService.getCoursesByTeacherId(teacherId);

        if (courses.isEmpty()) {
            // 如果列表为空，返回 404 Not Found 或 200 OK 带空列表 (推荐返回 200 OK 和空列表)
            return ResponseEntity.ok(courses);
        } else {
            return ResponseEntity.ok(courses); // HTTP 200 OK
        }
    }
}